package marcis.jarina.hibernate.dao;

import marcis.jarina.hibernate.entities.JavaClass;
import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;



@Repository(value = "classDao")
public class JavaClassDao {

    private static final Logger log = Logger.getLogger(JavaClassDao.class);

    @Autowired
    private SessionFactory sessionFactory;

    private Session session;


    public final SessionFactory getSessionFactory() {
        /*
        if (sessionFactory == null) {
            sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
        }
        */
        return sessionFactory;
    }

    public final Session getSession() {
        if (session == null || !session.isOpen()) {
            return getSessionFactory().openSession();
        }
        //return getSessionFactory().getCurrentSession();
        return session;
    }

    private void checkState() {
        //dataSource.getConnectionProperties().
    }

    public void save(JavaClass... entities) {
        for (JavaClass archive : entities) {
            getSession().save(archive);
        }
    }

    public List<JavaClass> getPagedList(int start, int maxResults) {
        try {
            String queryStr = "from JavaClass";
            Query query = getSession().createQuery(queryStr);
            query.setFirstResult(start);
            query.setMaxResults(maxResults);
            return (List<JavaClass>) query.list();
        } catch (HibernateException e) {
            log.error("error retrieving table: " + e.getMessage());
        }
        return null;
    }

    
    public JavaClass loadById(Integer id) {
        Criteria c = getSession().createCriteria(JavaClass.class);
        c.add(Restrictions.eq("id", id));
        return (JavaClass) c.uniqueResult();
    }

    
    public List<JavaClass> pagedFilter(String property, Object value, int startIndex, int maxResults) {
        Criteria c = getSession().createCriteria(JavaClass.class);
        // TODO the value should not be tested to null
        if (property != null && value != null) {
            c.add(Restrictions.eq(property, value));
        }
        c.setFirstResult(startIndex);
        c.setMaxResults(maxResults);
        return (List<JavaClass>) c.list();
    }


    
    public List<JavaClass> filter(String property, Object value) {
        Criteria c = getSession().createCriteria(JavaClass.class);
        if (property != null && value != null) {
            c.add(Restrictions.eq(property, value));
        }
        return (List<JavaClass>) c.list();
    }

    
    public Long getTotalCountOfClasses(long archiveId) {
        Query query = getSession().createQuery("select count(*) from JavaClass j where j.archive.id=" + archiveId);
        return (Long) query.uniqueResult();
    }

    
    public int deleteAll() {
        Query query = getSession().createQuery("delete from JavaClass");
        return query.executeUpdate();
    }
}
